package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

/**
 * Created by callxkj on 2018/1/11.
 * 预约详情 发送给客户的dialog
 */

public class ShareMsgDialogFragment extends DialogFragment implements View.OnClickListener {

    private RelativeLayout mRlsms;
    private RelativeLayout mRlClipboard;
    private View mRootView;
    private TextView mTvtitle;
    private TextView mTvfwd;
    private TextView mTvxxdz;
    private TextView mTvjcrq;
    private TextView mTvyysj;
    private TextView mTvjcjx;
    private String mTitle;
    private String mFwd;
    private String mXxdz;
    private String mJcrq;
    private String mYysj;
    private String mJcjx;
    private RelativeLayout mRlwechat;
    private RelativeLayout mRlqq;


    /**
     * @param title 标题
     * @param fwd   服务点
     * @param xxdz  详细地址
     * @param jcrq  检测日期
     * @param yysj  预约时间
     * @param jcjx  检测机型
     */
    public static ShareMsgDialogFragment newInstance(String title, String fwd, String xxdz,
                                                     String jcrq, String yysj, String jcjx) {
        ShareMsgDialogFragment dialogFragment = new ShareMsgDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("mFwd", fwd);
        bundle.putString("xxdz", xxdz);
        bundle.putString("jcrq", jcrq);
        bundle.putString("yysj", yysj);
        bundle.putString("jcjx", jcjx);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BottomDialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getArg();

        //加这句话去掉自带的标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mRootView = View.inflate(getContext(), R.layout.dialogfragment_sharemsg, null);
        return mRootView;

    }

    private void getArg() {
        Bundle arguments = getArguments();
        mTitle = arguments.getString("title");
        mFwd = arguments.getString("mFwd");
        mXxdz = arguments.getString("xxdz");
        mJcrq = arguments.getString("jcrq");
        mYysj = arguments.getString("yysj");
        mJcjx = arguments.getString("jcjx");

    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().setCanceledOnTouchOutside(true);

        mTvtitle = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_title);
        mTvfwd = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_fwd);
        mTvxxdz = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_xxdz);
        mTvjcrq = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_jcrq);
        mTvyysj = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_yysj);
        mTvjcjx = (TextView) mRootView.findViewById(R.id.yuxq_dialog_fragment_tv_jcjx);

        mRlwechat = (RelativeLayout) mRootView.findViewById(R.id.yuxq_dialog_fragment_rl_wechat);
        mRlqq = (RelativeLayout) mRootView.findViewById(R.id.yuxq_dialog_fragment_rl_qq);
        mRlsms = (RelativeLayout) mRootView.findViewById(R.id.yuxq_dialog_fragment_rl_sms);
        mRlClipboard = (RelativeLayout) mRootView.findViewById(R.id.yuxq_dialog_fragment_rl_clipboard);


        mTvtitle.setText(mTitle);
        mTvfwd.setText(mFwd);
        mTvxxdz.setText(mXxdz);
        mTvjcrq.setText(mJcrq);
        mTvyysj.setText(mYysj);
        mTvjcjx.setText(mJcjx);

        mRlwechat.setOnClickListener(this);
        mRlqq.setOnClickListener(this);
        mRlsms.setOnClickListener(this);
        mRlClipboard.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yuxq_dialog_fragment_rl_wechat: //微信好友
                toShareWechat();
                break;
            case R.id.yuxq_dialog_fragment_rl_qq: //QQ好友
                toShareQQ();
                break;
            case R.id.yuxq_dialog_fragment_rl_sms: //分享短信
                toSendSMS();
                break;
            case R.id.yuxq_dialog_fragment_rl_clipboard: //复制
                toClipboard();
                break;
        }
    }


    /**
     * 分享微信
     */
    private void toShareWechat() {
        new ShareAction(getActivity())
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withText(getMsg())
                .share();
    }

    /**
     * 分享qq
     */
    private void toShareQQ() {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, getMsg());
        sendIntent.setType("text/plain");
        try {
            sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
            if (chooserIntent == null) {
                return;
            }
            startActivity(chooserIntent);
        } catch (Exception e) {
            startActivity(sendIntent);
        }
    }

    /**
     * 分享短信
     */
    private void toSendSMS() {
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));
        intent.putExtra("sms_body", getMsg());
        startActivity(intent);
    }

    /**
     * 复制要分享的内容
     */
    private void toClipboard() {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", getMsg());
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);

        DZZWTools.showToast(AppUtil.getContext(), "复制成功", 0);
    }

    private String getMsg() {
        String title = mTitle + "\n\n";
        String msg1 = "服务点:   " + mFwd + "\n";
        String msg2 = "详细地址:   " + mXxdz + "\n";
        String msg3 = "检测日期:   " + mJcrq + "\n";
        String msg4 = "预约时间:   " + mYysj + "\n";
        String msg5 = "检测机型:   " + mJcjx;
        return title + msg1 + msg2 + msg3 + msg4 + msg5;
    }
}
